package com.shrct.businessprocess.customerservice;

import com.shrct.businessprocess.customerservice.RCProjectReference;
import com.shrct.businessprocess.customerservice.RCServiceContractDelegateDAO;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.events.RCCustomerContractEventsListener;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.event.BaseEntityEventsListener;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.customerservice.RCProjectDelegateInitializer")
public class RCProjectDelegateInitializer extends BaseEntityEventsListener<RCCustomerContract> implements RCCustomerContractEventsListener {
  @Autowired
  private RCServiceContractDelegateDAO serviceContractDelegateDAO;
  
  public void entityCreated(final RCCustomerContract entity) {
    final Procedure1<RCProjectReference> _function = new Procedure1<RCProjectReference>() {
        public void apply(final RCProjectReference it) {
        }
      };
    this.serviceContractDelegateDAO.createAsProjectReference(entity, _function);
    RCProject _project = entity.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
  }
  
  public void entityUpdated(final RCCustomerContract entity) {
    RCProject _project = entity.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
  }
  
  public void entityDeleted(final RCCustomerContract entity) {
    IGenericQuery<RCProjectReference> query=getObjectFactory().createGenericQuery(RCProjectReference.class,"select e from com.shrct.businessprocess.customerservice.impl.RCProjectReferenceImpl as e   where ((e.customerContract) = (:entity)) and  e.active = 1      ").setParameter("entity",entity);
    final RCProjectReference p = query.uniqueResult();
    this.serviceContractDelegateDAO.deleteProjectReference(p);
    RCProject _project = entity.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
  }
}
